package cn.bdqn.servlet;

import cn.bdqn.dao.HrInfoDao;
import cn.bdqn.dao.HrInfoDaoImpl;
import cn.bdqn.pojo.HrInfo;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author: 老鱼
 * @Date: 2019/1/5 15:15
 * @Description:
 */
@WebServlet(name = "AddServlet", urlPatterns = "/add")
public class AddServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //
        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        Integer age = Integer.parseInt(request.getParameter("age"));
        String rank = request.getParameter("rank");
        String dep = request.getParameter("department");

        PrintWriter out = response.getWriter();
        out.print("<script>");
        //表单内容检查
        if (name == null || name.equals("")) {
            out.print("alert('用户名不能为空');");
        } else if (age==null || age.equals("")) {
            out.print("alert('年龄不能为空');");
        } else if (sex.equals("请选择")) {
            out.print("alert('请选择性别');");
        } else if (rank.equals("请选择")) {
            out.print("alert('请选择等级');");
        } else if (dep.equals("请选择")) {
            out.print("alert('请选择部门');");
        } else{
            HrInfoDao dao=new HrInfoDaoImpl();
            dao.add(new HrInfo(name,sex,age,rank,dep));
            out.print("location.href='/index';");
        }
        out.print("</script>");

    }
}
